package com.hnucm.house.base;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;
/**
 * 绑定并解析View
 * @param <V>
 */
public class BasePresenter<V> {
    WeakReference<V> mWeakRef;

    //对订阅请求统一管理
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    //创建弱引用view
    public void attchView(V view){
        mWeakRef = new WeakReference<V>(view);
    }

    //将view取出
    protected V getView(){
        return mWeakRef == null ? null : mWeakRef.get();
    }

    //判断是否使用弱引用创建view
    protected boolean isViewAttached(){
        return mWeakRef != null && mWeakRef.get() != null;
    }

    //释放View并取消订阅
    public void detachView(){
        if(mWeakRef != null){
            mWeakRef.clear();
            mWeakRef = null;
        }
        mCompositeDisposable.clear();
    }
}
